package com.example.healthapp.utils;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.location.LocationManager;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Calendar;

public class Analysis {


    /**
     * 判断字符是否是汉字
     * @param str
     * @return
     */
    public static boolean containsChinese(String str) {
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (isChinese(c)) {
                return true;
            }
        }
        return false;
    }
    public static boolean isChinese(char c) {
        return c >= 0x4e00 && c <= 0x9fa5;
    }
    /**
     * 获取版本号名称
     *
     * @param context 上下文
     * @return
     */
    public static String getVerName(Context context) {
        String verName = "";
        try {
            verName = context.getPackageManager().
                    getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return verName;
    }

    public static byte[] toByteArray(String hexString) {
        hexString = hexString.toLowerCase();
        final byte[] byteArray = new byte[hexString.length() / 2];
        int k = 0;
        for (int i = 0; i < byteArray.length; i++) {
            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
            byteArray[i] = (byte) (high << 4 | low);
            k += 2;
        }
        return byteArray;
    }


    public static String getByteToString(byte[] bytes,String code,boolean isHex){
        try {
            if (isHex)
                return bytesToHexString(bytes);
            else
                return new String(bytes,0,bytes.length,code);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    //String字符串的互转
    public static String changeHexString(boolean isChangeHex,String string){
        if (string == null||string.isEmpty()){
            return "";
        }
        if (isChangeHex) {
            try {
                return bytesToHexString(string.getBytes("GBK"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return "";
        }else {
            return hexStringToString(string);
        }
    }

    //byte数组转String
    public static String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        int length = sb.length();
        if (length == 1||length == 0){
            return sb.toString();
        }
//        if (length%2==1){
//            sb.insert(length-1," ");
//            length= length-1;
//        }
//        for (int i = length;i>0;i=i-2){
//            sb.insert(i," ");
//        }
        return sb.toString();
    }


    public static String hexStringToString(String s) {
        if (s == null || s.equals("")) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(
                        s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "gbk");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    public static boolean detection(String str){
        int number = str.length();
        String scopeStr = "0123456789AaBbCcDdEeFf ";
        for (int i= 1;i<=number;i++){
            if (!scopeStr.contains(str.substring(i-1,i))){
                return true;
            }
        }
        return false;
    }

    public static void setHex(String text, int start, int before, int count, EditText editText){
        if (before > 0)
            return;
        if (text == null || text.equals(""))
            return;
        String temp = text.substring(start,start+count);
        if (detection(temp)){
            //Log.d("AppRun","temp.size is "+temp.length()+" count is "+count);
            String newStr = text.substring(0,start)+text.substring(start+count);
            editText.setText(newStr.toUpperCase());
            editText.setSelection(newStr.length());
        }else {
            editText.setText(text.toUpperCase());
            editText.setSelection(text.length());
        }
    }

    public static byte[] getBytes(String data,String code,boolean isHex){
        byte[] buff = null;
        if (!isHex) {
            try {
                buff =data.getBytes(code);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        else {
            buff = hexString2ByteArray(data);
        }
        return buff;
    }

    /**
     * 将16进制字符串转换为byte[]
     */
    public static byte[] hexString2ByteArray(String bs) {
        if (bs == null) {
            return null;
        }
        int bsLength = bs.length();
        if (bsLength % 2 != 0) {
            bs = "0"+bs;
            bsLength = bs.length();
        }
        byte[] cs = new byte[bsLength / 2];
        String st;
        for (int i = 0; i < bsLength; i = i + 2) {
            st = bs.substring(i, i + 2);
            cs[i / 2] = (byte) Integer.parseInt(st, 16);
        }
        return cs;
    }


    //按间隔取出，例如analysis("123aa456aa789aa000",2,"aa") ->"789"
    //最多3个间隔，4个字符串，从0开始取
    public static String analysis(final String data,int number,String key){
        if (number == 0)
            return data.substring(0,data.indexOf(key));
        if (number == 1) {
            String string = data.substring(data.indexOf(key) + key.length());
            return string.substring(0,string.indexOf(key));
        }
        if (number == 2){
            int length2 = analysis(data,1,key).length();
            int length1 = analysis(data,0,key).length();
            String string = data.substring(length1+length2+key.length()*2);
            return string.substring(0,string.indexOf(key));
        }else {
            int length = analysis(data,0,key).length()+analysis(data,1,key).length()+analysis(data,2,key).length()+key.length()*3;
            return data.substring(length);
        }
    }

    private static String hexString = "0123456789ABCDEF";
    /**
     * 将字符串编码成16进制数字,适用于所有字符（包括中文）
     */
    public static String encode(String str) {
        // 根据默认编码获取字节数组
        byte[] bytes = str.getBytes(Charset.forName("GBK"));
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        // 将字节数组中每个字节拆解成2位16进制整数
        for (byte aByte : bytes) {
            sb.append(hexString.charAt((aByte & 0xf0) >> 4));
            sb.append(hexString.charAt((aByte & 0x0f)));
        }
        return sb.toString();
    }

    public static final String K(Context context, String str) {
        e(context, "<this>");
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        if (!N(context)) {
            c(str);
            return str;
        }
//        if (i.c.a.c.n.c.a(str) < 0.5d) {
//            c(str);
//            return str;
//        }
        String L2 = L1("[{ \"from\": \"\u200b\", \"to\": \"\" }, { \"from\": \"(ွ|ႇ)\", \"to\": \"ှ\" }, { \"from\": \"ြ\", \"to\": \"ွ\" }, { \"from\": \"(ျ|ၾ|ၿ|ႀ|ႁ|ႂ|ႃ|ႄ)\", \"to\": \"ြ\" }, { \"from\": \"(်|ၽ)\", \"to\": \"ျ\" }, { \"from\": \"္\", \"to\": \"်\" }, { \"from\": \"(ၦ|ၧ)\", \"to\": \"္ဆ\" }, { \"from\": \"ၪ\", \"to\": \"ဉ\" }, { \"from\": \"ၫ\", \"to\": \"ည\" }, { \"from\": \"ၬ\", \"to\": \"္ဋ\" }, { \"from\": \"ၭ\", \"to\": \"္ဌ\" }, { \"from\": \"ၮ\", \"to\": \"ဍ္ဍ\" }, { \"from\": \"ၯ\", \"to\": \"ဍ္ဎ\" }, { \"from\": \"ၰ\", \"to\": \"္ဏ\" }, { \"from\": \"(ၱ|ၲ)\", \"to\": \"္တ\" }, { \"from\": \"ၠ\", \"to\": \"္က\" }, { \"from\": \"ၡ\", \"to\": \"္ခ\" }, { \"from\": \"ၢ\", \"to\": \"္ဂ\" }, { \"from\": \"ၣ\", \"to\": \"္ဃ\" }, { \"from\": \"ၥ\", \"to\": \"္စ\" }, { \"from\": \"ၨ\", \"to\": \"္ဇ\" }, { \"from\": \"ၩ\", \"to\": \"္ဈ\" }, { \"from\": \"(ၳ|ၴ)\", \"to\": \"္ထ\" }, { \"from\": \"ၵ\", \"to\": \"္ဒ\" }, { \"from\": \"ၶ\", \"to\": \"္ဓ\" }, { \"from\": \"ၷ\", \"to\": \"္န\" }, { \"from\": \"ၸ\", \"to\": \"္ပ\" }, { \"from\": \"ၹ\", \"to\": \"္ဖ\" }, { \"from\": \"ၺ\", \"to\": \"္ဗ\" }, { \"from\": \"ၼ\", \"to\": \"္မ\" }, { \"from\": \"ႅ\", \"to\": \"္လ\" }, { \"from\": \"ဳ\", \"to\": \"ု\" }, { \"from\": \"ဴ\", \"to\": \"ူ\" }, { \"from\": \"ဿ\", \"to\": \"ူ\" }, { \"from\": \"ႆ\", \"to\": \"ဿ\" }, { \"from\": \"ံႈ\", \"to\": \"ႈံ\" }, { \"from\": \"ႈ\", \"to\": \"ှု\" }, { \"from\": \"ႉ\", \"to\": \"ှူ\" }, { \"from\": \"ႊ\", \"to\": \"ွှ\" }, { \"from\": \"ျၤ\", \"to\": \"ၤျ\" }, { \"from\": \"(ေ)?([က-အ])ၤ\", \"to\": \"င်္$1$2\" }, { \"from\": \"(ေ)?([က-အ])ႋ\", \"to\": \"င်္$1$2ိ\" }, { \"from\": \"(ေ)?([က-အ])ႌ\", \"to\": \"င်္$1$2ီ\" }, { \"from\": \"(ေ)?([က-အ])ႍ\", \"to\": \"င်္$1$2ံ\" }, { \"from\": \"ႎ\", \"to\": \"ိံ\" }, { \"from\": \"ႏ\", \"to\": \"န\" }, { \"from\": \"႐\", \"to\": \"ရ\" }, { \"from\": \"႑\", \"to\": \"ဏ္ဍ\" }, { \"from\": \"မာ(ၻ|႓)\", \"to\": \"မ္ဘာ\" }, { \"from\": \"(ၻ|႓)\", \"to\": \"္ဘ\" }, { \"from\": \"(႔|႕)\", \"to\": \"့\" }, { \"from\": \"([က-အ])့ဲ\", \"to\": \"$1ဲ့\" }, { \"from\": \"႖\", \"to\": \"္တွ\" }, { \"from\": \"႗\", \"to\": \"ဋ္ဋ\" }, { \"from\": \"ြ([က-အ])([က-အ])?\", \"to\": \"$1ြ$2\" }, { \"from\": \"([က-အ])ြ်\", \"to\": \"ြ$1်\" }, { \"from\": \"၇(?=[ာ-ူဲံ-းွး])\", \"to\": \"ရ\" }, { \"from\": \"ေ၇\", \"to\": \"ေရ\" }, { \"from\": \"၀(ီ|ု|ို|ူ|ံ|ွ|ှ)\", \"to\": \"ဝ$1\" }, { \"from\": \"([^၀၁၂၃၄၅၆၇၈၉])၀ါ\", \"to\": \"$1ဝါ\" }, { \"from\": \"([၀၁၂၃၄၅၆၇၈၉])၀ါ(?!း)\", \"to\": \"$1ဝါ\" }, { \"from\": \"^၀(?=ါ)\", \"to\": \"ဝ\" }, { \"from\": \"၀ိ(?! ?/)\", \"to\": \"ဝိ\" }, { \"from\": \"([^၀-၉])၀([^၀-၉ ]|[၊။])\", \"to\": \"$1ဝ$2\" }, { \"from\": \"([^၀-၉])၀(?=[\\f\\n\\r])\", \"to\": \"$1ဝ\" }, { \"from\": \"([^၀-၉])၀$\", \"to\": \"$1ဝ\" }, { \"from\": \"ေ([က-အ])(ှ)?(ျ)?\", \"to\": \"$1$2$3ေ\" }, { \"from\": \"([က-အ])ေ([ျြွှ]+)\", \"to\": \"$1$2ေ\" }, { \"from\": \"ဲွ\", \"to\": \"ွဲ\" }, { \"from\": \"([ိီ])ျ\", \"to\": \"ျ$1\" }, { \"from\": \"ွျ\", \"to\": \"ျွ\" }, { \"from\": \"့်\", \"to\": \"့်\" }, { \"from\": \"ု(ိ|ီ|ံ|့)ု\", \"to\": \"ု$1\" }, { \"from\": \"(ု|ူ)(ိ|ီ)\", \"to\": \"$2$1\" }, { \"from\": \"(ှ)(ျ|ြ)\", \"to\": \"$2$1\" }, { \"from\": \"ဥ(?=[့]?[်ာ])\", \"to\": \"ဉ\" }, { \"from\": \"ဦ\", \"to\": \"ဦ\" }, { \"from\": \"စျ\", \"to\": \"ဈ\" }, { \"from\": \"ံ(ု|ူ)\", \"to\": \"$1ံ\" }, { \"from\": \"ေ့ှ\", \"to\": \"ှေ့\" }, { \"from\": \"ေှာ\", \"to\": \"ှော\" }, { \"from\": \"ၚ\", \"to\": \"ါ်\" }, { \"from\": \"ေျှ\", \"to\": \"ျှေ\" }, { \"from\": \"(ိ|ီ)(ွ|ှ)\", \"to\": \"$2$1\" }, { \"from\": \"ာ္([က-အ])\", \"to\": \"္$1ာ\" }, { \"from\": \"ြင်္([က-အ])\", \"to\": \"င်္$1ြ\" }, { \"from\": \"္ြ်္([က-အ])\", \"to\": \"်္$1ြ\" }, { \"from\": \"ြ္([က-အ])\", \"to\": \"္$1ြ\" }, { \"from\": \"ံ္([က-အ])\", \"to\": \"္$1ံ\" }, { \"from\": \"႒\", \"to\": \"ဋ္ဌ\" }, { \"from\": \"၎\", \"to\": \"၎င်း\" }, { \"from\": \"၀(ါ|ာ|ံ)\", \"to\": \"ဝ$1\" }, { \"from\": \"ဥ္\", \"to\": \"ဉ္\" }, { \"from\": \"([က-အ])ြေွ\", \"to\": \"$1ြွေ\" }, { \"from\": \"([က-အ])ျေွ(ှ)?\", \"to\": \"$1ျွ$2ေ\" }, { \"from\": \"([က-အ])ွေျ\", \"to\": \"$1ျွေ\" }, { \"from\": \"([က-အ])ေ(္[က-အ])\", \"to\": \"$1$2ေ\" }, { \"from\": \"း်\", \"to\": \"်း\" }, { \"from\": \"ိ်|်ိ\", \"to\": \"ိ\" }, { \"from\": \"ို်\", \"to\": \"ို\" }, { \"from\": \" ့\", \"to\": \"့\" }, { \"from\": \"့ံ\", \"to\": \"ံ့\" }, { \"from\": \" ့\", \"to\": \"့\" }, { \"from\": \"[ိ]+\", \"to\": \"ိ\" }, { \"from\": \"[်]+\", \"to\": \"်\" }, { \"from\": \"[ွ]+\", \"to\": \"ွ\" }, { \"from\": \"[့]+\", \"to\": \"့\" }, { \"from\": \"[ီ]+\", \"to\": \"ီ\" }, { \"from\": \"ိီ|ီိ\", \"to\": \"ီ\" }, { \"from\": \"ုိ\", \"to\": \"ို\" }, { \"from\": \"့့\", \"to\": \"့\" }, { \"from\": \"ဲဲ\", \"to\": \"ဲ\" }, { \"from\": \"၄င်း\", \"to\": \"၎င်း\" }, { \"from\": \"([ိီ])္([က-အ])\", \"to\": \"္$2$1\" }, { \"from\": \"ံွ\", \"to\": \"ွံ\" }]", str);
        d(L2, "zg2uni(input)");
        return L2;
    }

    public static void d(Object obj, String str) {
        if (obj != null) {
            return;
        }
        NullPointerException nullPointerException = new NullPointerException(q(str, " must not be null"));
        i(nullPointerException, Analysis.class.getName());
        throw nullPointerException;
    }

    public static String q(String str, String str2) {
        return str + str2;
    }

    public static void e(Object obj, String str) {
        if (obj != null) {
            return;
        }
        NullPointerException nullPointerException = new NullPointerException(h(str));
        i(nullPointerException, Analysis.class.getName());
        throw nullPointerException;
    }

    public static String h(String str) {
        StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[4];
        String className = stackTraceElement.getClassName();
        String methodName = stackTraceElement.getMethodName();
        return "Parameter specified as non-null is null: method " + className + "." + methodName + ", parameter " + str;
    }

    public static <T extends Throwable> T i(T t2, String str) {
        StackTraceElement[] stackTrace = t2.getStackTrace();
        int length = stackTrace.length;
        int i2 = -1;
        for (int i3 = 0; i3 < length; i3++) {
            if (str.equals(stackTrace[i3].getClassName())) {
                i2 = i3;
            }
        }
        t2.setStackTrace((StackTraceElement[]) Arrays.copyOfRange(stackTrace, i2 + 1, length));
        return t2;
    }

    public static final boolean N(Context context) {
        int i2 = Build.VERSION.SDK_INT;
        Configuration configuration = context.getResources().getConfiguration();
        return a((i2 >= 24 ? configuration.getLocales().get(0) : configuration.locale).getLanguage(), "my");
    }

    public static boolean a(Object obj, Object obj2) {
        if (obj == null) {
            return obj2 == null;
        }
        return obj.equals(obj2);
    }

    public static void c(Object obj) {
        if (obj != null) {
            return;
        }
        NullPointerException nullPointerException = new NullPointerException();
        i(nullPointerException, Analysis.class.getName());
        throw nullPointerException;
    }

    public static String L1(String str, String str2) {
        try {
            JSONArray jSONArray = new JSONArray(str);
            int length = jSONArray.length();
            str2 = str2.replace("null", "\uffff\uffff");
            for (int i2 = 0; i2 < length; i2++) {
                JSONObject jSONObject = jSONArray.getJSONObject(i2);
                str2 = str2.replaceAll(jSONObject.getString("from"), jSONObject.getString("to")).replace("null", "");
            }
        } catch (JSONException e2) {
            e2.printStackTrace();
        }
        return str2.replace("\uffff\uffff", "null");
    }

}
